# 测试添加文件类型的API端点

$baseUrl = "http://localhost:5269/api/file"
$endpoint = "$baseUrl/types"

# 创建一个新的文件类型名称（添加随机数避免冲突）
$randomId = Get-Random -Minimum 1000 -Maximum 9999
$testTypeName = "测试类型$randomId"
$testMemoryStrength = 1.2

Write-Host "测试添加文件类型API：$endpoint"
Write-Host "测试数据：类型名称='$testTypeName', 记忆强度=$testMemoryStrength"

# 构建请求体
$requestBody = @{
    TypeName = $testTypeName
    MemoryStrength = $testMemoryStrength
}
$requestBodyJson = ConvertTo-Json $requestBody

# 发送POST请求
try {
    $response = Invoke-RestMethod -Uri $endpoint -Method Post -Body $requestBodyJson -ContentType "application/json"
    
    Write-Host "`n✅ 请求成功！状态码：201 Created"
    Write-Host "返回数据："
    $response | ConvertTo-Json -Depth 10 | Write-Host -ForegroundColor Green
    
    # 验证添加的文件类型是否存在
    Write-Host "`n🔍 验证添加的文件类型是否存在..."
    $allTypes = Invoke-RestMethod -Uri $endpoint -Method Get
    $addedType = $allTypes | Where-Object { $_.TypeName -eq $testTypeName }
    
    if ($addedType) {
        Write-Host "✅ 文件类型 '$testTypeName' 已成功添加到系统中！" -ForegroundColor Green
        Write-Host "添加的类型详情："
        $addedType | ConvertTo-Json -Depth 10 | Write-Host -ForegroundColor Green
    } else {
        Write-Host "❌ 未找到添加的文件类型 '$testTypeName'！" -ForegroundColor Red
    }
} catch {
    Write-Host "`n❌ 请求失败！" -ForegroundColor Red
    if ($_.Exception.Response) {
        $statusCode = $_.Exception.Response.StatusCode.value__
        $statusDescription = $_.Exception.Response.StatusDescription
        Write-Host "状态码：$statusCode ($statusDescription)" -ForegroundColor Red
        
        try {
            $errorResponse = $_.ErrorDetails.Message | ConvertFrom-Json
            Write-Host "错误信息：$($errorResponse.Message)" -ForegroundColor Red
            if ($errorResponse.Error) {
                Write-Host "详细错误：$($errorResponse.Error)" -ForegroundColor Red
            }
        } catch {
            Write-Host "无法解析错误响应：$($_.ErrorDetails.Message)" -ForegroundColor Red
        }
    } else {
        Write-Host "异常信息：$($_.Exception.Message)" -ForegroundColor Red
    }
}

Write-Host "`n📋 测试完成！"